﻿cmake_minimum_required(VERSION 3.26)
set(CUR_PROJECT_NAME pack_tools)
project(${CUR_PROJECT_NAME} VERSION 1.0.0
    DESCRIPTION "Envove Pack Tool"
    LANGUAGES CXX
)

set(CMAKE_CXX_STANDARD 20)
# enable moc
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS 
    Core 
    Widgets 
    Gui
)
# 在项目声明之后、添加可执行文件之前添加
if(MSVC)
    # 强制使用静态链接运行时库
    set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
    
    # 替代方案：直接修改编译选项
    foreach(flag_var 
            CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
            CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
        if(${flag_var} MATCHES "/MD")
            string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
        endif()
    endforeach()
endif()

string(TIMESTAMP BUILD_DATE "%Y%m%d")

find_package(nlohmann_json CONFIG REQUIRED)
# 查找XPACK库
find_path(XPACK_INCLUDE_DIRS "xpack/config.h")
# 查找libzip库
find_package(libzip CONFIG REQUIRED)


# 库输出include文件
SET(${CUR_PROJECT_NAME}_INCLUDE
    src/packconfig.h
    src/mainwindow.h
    src/packworker.h
   )

SET(${CUR_PROJECT_NAME}_SRC
    src/main.cpp
    src/packconfig.cpp
    src/mainwindow.cpp
    src/packworker.cpp
   )

add_executable(${CUR_PROJECT_NAME}
    ${${CUR_PROJECT_NAME}_INCLUDE}
    ${${CUR_PROJECT_NAME}_SRC}
)

#[[ 为Release版本设置带版本号的输出文件名
# 支持多配置生成器（如Visual Studio）和单配置生成器
if(CMAKE_CONFIGURATION_TYPES)
    # 多配置生成器（如Visual Studio）
    set_target_properties(${CUR_PROJECT_NAME} PROPERTIES
        OUTPUT_NAME_RELEASE "${CUR_PROJECT_NAME}_v${PROJECT_VERSION}"
        OUTPUT_NAME_DEBUG "${CUR_PROJECT_NAME}"
        OUTPUT_NAME_RELWITHDEBINFO "${CUR_PROJECT_NAME}_v${PROJECT_VERSION}"
        OUTPUT_NAME_MINSIZEREL "${CUR_PROJECT_NAME}_v${PROJECT_VERSION}"
    )
else()
    # 单配置生成器
    if(CMAKE_BUILD_TYPE STREQUAL "Release" OR 
       CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo" OR 
       CMAKE_BUILD_TYPE STREQUAL "MinSizeRel")
        set_target_properties(${CUR_PROJECT_NAME} PROPERTIES
            OUTPUT_NAME "${CUR_PROJECT_NAME}_v${PROJECT_VERSION}"
        )
    endif()
endif()
]]#

if(WIN32)
    # 设置为Windows GUI应用程序，在代码中动态分配控制台
    set_target_properties(${CUR_PROJECT_NAME} PROPERTIES
        WIN32_EXECUTABLE ON  # 设置为Windows GUI应用程序
    )
endif()

# 设置安装规则
include(GNUInstallDirs)

target_link_libraries(${CUR_PROJECT_NAME} PRIVATE
    Qt${QT_VERSION_MAJOR}::Core
    Qt${QT_VERSION_MAJOR}::Widgets
    Qt${QT_VERSION_MAJOR}::Gui
    nlohmann_json::nlohmann_json
    libzip::zip
)


# 包含头文件路径（PUBLIC确保下游项目也能访问）
message(STATUS "XPACK_INCLUDE_DIRS: ${XPACK_INCLUDE_DIRS}")
message(STATUS "CMAKE_CURRENT_SOURCE_DIR: ${CMAKE_CURRENT_SOURCE_DIR}")
target_include_directories(${CUR_PROJECT_NAME} PRIVATE
    $<BUILD_INTERFACE:${XPACK_INCLUDE_DIRS}>
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

target_compile_definitions(${CUR_PROJECT_NAME} PRIVATE
    EV_FIREWAVE_TOOL_VERSION="${PROJECT_VERSION}"
    EV_FIREWAVE_TOOL_VERSION_MAJOR=${PROJECT_VERSION_MAJOR}
    EV_FIREWAVE_TOOL_VERSION_MINOR=${PROJECT_VERSION_MINOR}
    EV_FIREWAVE_TOOL_VERSION_PATCH=${PROJECT_VERSION_PATCH}
    EV_FIREWAVE_TOOL_BUILD_DATE="${BUILD_DATE}"
    # 为资源文件添加版本信息宏
    RC_VERSION_MAJOR=${PROJECT_VERSION_MAJOR}
    RC_VERSION_MINOR=${PROJECT_VERSION_MINOR}
    RC_VERSION_PATCH=${PROJECT_VERSION_PATCH}
    RC_VERSION_BUILD=0
    RC_VERSION_STRING="${PROJECT_VERSION}"
    RC_PRODUCT_NAME="Envove Pack Tool"
    RC_INTERNAL_NAME="envove_pack_tool"
    RC_ORIGINAL_FILENAME="envove_pack_tool.exe"
    RC_FILE_DESCRIPTION="Envove Print Head Fire Wave Define Tool"
    RC_COMPANY_NAME="Envove Inc"
    RC_COPYRIGHT="Copyright (C) 2025 Envove Inc. All rights reserved."
)

# 设置编译选项以支持UTF-8编码
if(MSVC)
    target_compile_options(${CUR_PROJECT_NAME} PRIVATE /utf-8)
else()
    target_compile_options(${CUR_PROJECT_NAME} PRIVATE -finput-charset=UTF-8 -fexec-charset=UTF-8)
endif()

install(TARGETS ${CUR_PROJECT_NAME}
    EXPORT ${CUR_PROJECT_NAME}Targets 
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}  # 如/usr/local/lib
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}    
    PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${CUR_PROJECT_NAME}
)
# 导出配置供find_package使用
install(EXPORT ${CUR_PROJECT_NAME}Targets 
    FILE ${CUR_PROJECT_NAME}Targets.cmake
    NAMESPACE ${CUR_PROJECT_NAME}::
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${CUR_PROJECT_NAME}
)

#add_subdirectory(tests)